import axios from 'axios'
import { Message,Notification } from 'element-ui'
import store from '@/store'
// import { getToken } from '@/utils/auth'
import qs from 'qs'
// 创建axios实例

const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 10000, // 请求超时时间
  withCredentials:true
})
// axios.defaults.withCredentials=true
// request拦截器
service.interceptors.request.use(function (config) {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
    if(config.method === 'post') {
        config.data = qs.stringify( {
            ...config.data
        })
    }
    return config;
  }, function (error) {
    loadinginstace.close()
    return Promise.reject(error);
  })

// respone拦截器
service.interceptors.response.use(
  response =>{
    if(response.status==404){
      Message({
        message: '该地址不存在',
        type: 'error'
      })
      return Promise.reject('error');
    }
    if(response.status==500){
      Message({
        message: '系统错误',
        type: 'error'
      })
      return Promise.reject('error');
    }
    if(response.status==200){
      if(response.data.success){
        // Message({
        //   message: response.data.msg,
        //   type: 'success'
        // })
        return  Promise.resolve(response.data);
      }else{
        Message({
          message: response.data.msg,
          type: 'error'
        })
        return Promise.reject(response.data.msg)
      }
    }
  },
  error => {
    console.log('err' + error)// for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  })

export default service
